\documentclass{beamer}
\usetheme{default}
\usepackage[italian]{babel}
%\usepackage{t1enc}
\usepackage{pgfpages}
\usepackage{listings}
%\pgfpagelayout{2 on 1}[a4paper]

%\usepackage{../../common/espacs}
\lstset
{
  language=[ISO]C++,                       % The default language
  basicstyle=\sf,                          % The basic style
  keywordstyle=\color{blue}\bfseries,      % Set keyword style
  commentstyle=\color{darkgreen}\itshape,  % Set comment style
  extendedchars=true                       % Allow extended characters
}

\setbeamercovered{transparent}

\begin{document}

%---------------------------------------------------------------------------------

\begin{frame}

    Little introduction to the compilation of a C++ code

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    Let's consider the following program

    \lstset{basicstyle=\scriptsize\sf}
    \lstinputlisting{./main.cpp}
    \lstset{basicstyle=\sf}

    \vspace{.3cm}
    that is saved as \verb1main.cpp1.

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    The code can be compiled and executed with the following commands

    \begin{verbatim}
        g++ -c main.cpp
        g++ -o main main.o
        ./main
    \end{verbatim}

    \vspace{.3cm}
    To get
    \begin{verbatim}
        Hello World!
    \end{verbatim}

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    Another way to compile, that does not leave an object file, is by giving 
    directly
    \begin{verbatim}
        g++ -o main main.cpp
        ./main
    \end{verbatim}

    \vspace{.3cm}
    In this case the compilation and linking phase are exectued in sequence
    with a unique command.

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    There are many flags that can be passed to the compiler to get different
    behaviors

    \begin{itemize}
        \item \verb1-Wall1 to get almost all warnings
        \item \verb1-On1 to set the level of optimization that the compiler is
        allowed to try
        \item \verb1-g1 to generate debugging symbols inside the code
    \end{itemize}

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    The flag \verb1-Wall1 tells the compiler to give feedback on the warnings.
    If we use it with our \verb1main.cpp1

    \begin{verbatim}
        g++ -o main main.cpp -Wall
    \end{verbatim}

    \vspace{.3cm}
    We get
    \begin{verbatim}
        main.cpp: In function 'int main()':
        main.cpp:7: warning: unused variable 'i'
    \end{verbatim}

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    The flag \verb1-O1 must be followed by a number between $0$ and $3$, in
    order to get a more efficient code (using more time to compile).

    \vspace{.3cm}
    In details

    \begin{itemize}

        \item \verb1-O01 the compiler is not allowed to execute any optimization

        \item \verb2-O12 the compiler tries to reduce the dimension of the code
            without executing any optimization that requires a long time.

        \item \verb1-O21 the compiler is allowed to use a wider set of
            optimizations that lead to a good compromise between space and
            speed. The time required to compile is increased and the code
            performs better.

        \item \verb1-031 the compiler is allowed to use all optimizations to
            get the fastest code available.

    \end{itemize}

\end{frame}

%---------------------------------------------------------------------------------

\begin{frame}[fragile]

    \frametitle{How to compile a C++ code}

    \vspace{.3cm}
    More than one flag can be used simultaneously, the order is not important.
    \begin{verbatim}
        g++ -o main main.cpp -Wall -O2
    \end{verbatim}

\end{frame}

%---------------------------------------------------------------------------------

\end{document}
